package dao

import (
	"context"
	"fabric-explorer/entity"
	"fabric-explorer/tools"
	"fmt"
	"time"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo/options"
)

const (
	BLOCK_COLLECTION_NAME = "block"
)

//插入block
func InsertBlock(block entity.Block) {
	// tools.InitDB()
	collection := tools.GetCollection(BLOCK_COLLECTION_NAME)
	insertResult, err := collection.InsertOne(context.TODO(), block)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Inserted a single document: ", insertResult.InsertedID)
}

//批量插入
func InsertBlockBatch(blockList []interface{}) {
	collection := tools.GetCollection(BLOCK_COLLECTION_NAME)
	insertManyResult, err := collection.InsertMany(context.TODO(), blockList)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Inserted multiple documents: ", insertManyResult.InsertedIDs)
}

//查询list
func FindBlockList(selecter bson.D) []entity.Block {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	collection := tools.GetCollection(BLOCK_COLLECTION_NAME)
	cur, err := collection.Find(ctx, selecter)
	if err != nil {
		fmt.Println(err)
	}
	defer cur.Close(ctx)

	blockList := make([]entity.Block, 0)
	for cur.Next(ctx) {
		block := &entity.Block{}
		err := cur.Decode(block)
		if err != nil {
			fmt.Println(err)
		}
		blockList = append(blockList, *block)
	}
	if err := cur.Err(); err != nil {
		fmt.Println(err)
	}
	return blockList
}

//查询page
func FindBlockListPage(selecter bson.D, options *options.FindOptions) []entity.Block {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	collection := tools.GetCollection(BLOCK_COLLECTION_NAME)
	cur, err := collection.Find(ctx, selecter, options)
	if err != nil {
		fmt.Println(err)
	}
	defer cur.Close(ctx)

	blockList := make([]entity.Block, 0)
	for cur.Next(ctx) {
		block := &entity.Block{}
		err := cur.Decode(block)
		if err != nil {
			fmt.Println(err)
		}
		blockList = append(blockList, *block)
	}
	if err := cur.Err(); err != nil {
		fmt.Println(err)
	}
	return blockList
}

//查询一个block
func FindBlockOne(selecter bson.D) entity.Block {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	collection := tools.GetCollection(BLOCK_COLLECTION_NAME)
	oneResult := collection.FindOne(ctx, selecter)
	block := &entity.Block{}
	err := oneResult.Decode(block)
	if err != nil {
		fmt.Println(err)
	}
	return *block
}

//查询index
func GetBlockNum(selecter bson.D) int64 {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	collection := tools.GetCollection(BLOCK_COLLECTION_NAME)
	num, err := collection.CountDocuments(ctx, selecter)
	if err != nil {
		fmt.Println(err)
	}
	return num
}

//删除block
func DeleteBlock(selecter bson.D) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	collection := tools.GetCollection(BLOCK_COLLECTION_NAME)
	_, err := collection.DeleteMany(ctx, selecter)
	if err != nil {
		fmt.Println(err)
	}
}
